<?php

use console\db\Migration;

/**
 * 创建用户签到相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m180904_074432_t_signin extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 用户签到表
         * @table `user_signin`.
         */
        $this->tableBuilder([
            'name' => 'user_signin',
            'comment' => '用户签到表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'amount' => $this->smallInteger(6)->unsigned()->notNull()->defaultValue(0)->comment('总签到数'),
                'serial' => $this->smallInteger(6)->unsigned()->notNull()->defaultValue(0)->comment('最近连续签到数'),
                'max_serial' => $this->smallInteger(6)->unsigned()->notNull()->defaultValue(0)->comment('最长连续签到数'),
                'repairs' => $this->tinyint(4)->unsigned()->notNull()->defaultValue(0)->comment('历史补签数'),
                'last_time' => $this->unixTimestamp()->comment('最后签到时间'),
                'recent' => $this->bigInteger(20)->unsigned()->notNull()->defaultValue(0)->comment('最近周期签到历史'),
                'current_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('当前周期ID'),
                'created_at' => $this->unixTimestamp()->comment('首次签到时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ],
                [
                    'columns' => 'last_time',
                ],
            ]
        ]);

        /**
         * 用户签到历史表
         * @table `user_signin_history`.
         */
        $this->tableBuilder([
            'name' => 'user_signin_history',
            'comment' => '用户签到历史表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'days' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('周期天数'),
                'sign_days' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('周期签到数'),
                'first_date' => $this->date()->comment('周期内首次签到日期'),
                'last_date' => $this->date()->comment('周期内最后一次签到日期'),
                'history' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('周期签到历史'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ],
                [
                    'columns' => 'updated_at',
                ],
            ]
        ]);


        /**
         * 用户签到日志表
         * @table `user_signin_log`.
         */
        $this->tableBuilder([
            'name' => 'user_signin_log',
            'comment' => '用户签到日志表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'ip' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('签到IP'),
                'serial' => $this->smallInteger(6)->unsigned()->notNull()->defaultValue(0)->comment('当前连续签到数'),
                'created_at' => $this->unixTimestamp()->comment('签到时间'),
                'repaired_at' => $this->unixTimestamp()->comment('补签时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ],
                [
                    'columns' => 'created_at',
                ],
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%user_signin_log}}');
        $this->dropTable('{{%user_signin_history}}');
        $this->dropTable('{{%user_signin}}');
    }

}
